﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Common;
using StoragePeriod.Data;
using StoragePeriod.Web.Common;
using System.Text;
using System.Collections;
using StoragePeriod.Web.UserControls;
using System.Data;
using StoragePeriod.Web.NopiExcel;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.FoodTesting;

namespace StoragePeriod.Web.FoodTesting
{
    public partial class Carbohydrates : SecurityPage
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录操作
        T_CarbohydratesData tCarbohydratesData = new T_CarbohydratesData();     //检测记录操作
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作
        T_FoodDirection tFoodDirection = new T_FoodDirection();//检测说明

        public override string PrivCode()
        {
            return "bea8d26b7a5230df";
        }

        public static string ShowValue(object o)
        {
            string s = "";
            if (o != null && o.ToString() != "0")
                s = o.ToString();
            return s;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ((ScriptManager)(Master.FindControl("ContentScriptManager").FindControl("ScriptManager1"))).RegisterPostBackControl(Btn_Export); //未了解决使用 UpLoadPanl 而无法输出文件的问题

            LeftTree.SelectedChanging += new MaterialTree.TreeEventDelegate(LeftTree_SelectedChanging);
            LeftLable.Text = "碳水化合物测定";

            if (IsPostBack) return;
        }

        protected void LeftTree_SelectedChanging(Dictionary<string, string> dic)
        {
            if (dic.Count > 0)
            {
                List<E_FoodRecord> List = new List<E_FoodRecord>();
                dic.ToList().ForEach(p =>
                {
                    E_FoodRecord s = new E_FoodRecord();
                    s.RawID = Convert.ToInt32(p.Key);
                    s.RawName = p.Value;
                    s.FoodResult = "";
                    List.Add(s);
                });
                Session["Carbohydrates_RawList"] = List;
                this.Rep_DataList.DataSource = List;
                this.Rep_DataList.DataBind();

                if (this.HidPageId.Value == "1")
                {
                    Assdatalist = new List<List<AssayDataList>>();
                    string SelectRaw = "";
                    string SelectRawName = "";
                    dic.ToList().ForEach(p =>
                    {
                        List<AssayDataList> datalist = new List<AssayDataList>();
                        SelectRaw += p.Key + ",";
                        SelectRawName += p.Value + ",";
                        datalist.Add(new AssayDataList() { RawName = p.Value, IsSelectRaw = 1 });
                        Assdatalist.Add(datalist);
                    });
                    this.HidRawListId.Value = SelectRaw.TrimEnd(',');
                    this.lab_RawListName.Text = SelectRawName.TrimEnd(',');
                }
            }

            if (this.HidPageId.Value == "1")
            {
                UtilityUI.Load(this.UpdatePanel1, "SetShowPage()");
            }
            else
            {
                UtilityUI.Init(this.UpdatePanel1);
            }
        }


        protected void Rep_DataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int RawID = int.Parse(e.CommandArgument.ToString());
                List<E_FoodRecord> List = (List<E_FoodRecord>)Session["Carbohydrates_RawList"];
                E_FoodRecord Carbohydrates = List.Where(p => p.RawID == RawID).First();
                List.Remove(Carbohydrates);
                Session["Carbohydrates_RawList"] = List;
                this.Rep_DataList.DataSource = List;
                this.Rep_DataList.DataBind();
                UtilityUI.Init(this.UpdatePanel1);
            }
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Session["Carbohydrates_RawList"] == null)
            {
                UtilityUI.Alert(this.UpdatePanel1, "请选择原料！");
                return;
            }

            DateTime date = Convert.ToDateTime(this.txt_date.Text.Trim());
            List<E_FoodRecord> List = (List<E_FoodRecord>)Session["Carbohydrates_RawList"];
            List.ForEach(p =>
            {
                E_FoodRecord eFoodRecord = new E_FoodRecord();
                eFoodRecord.RawID = p.RawID;
                eFoodRecord.FoodTypeID = 3;//水分含量检测
                eFoodRecord.FoodTime = date;
                eFoodRecord = tFoodRecord.GetFoodRecord(eFoodRecord);
                if (eFoodRecord != null)
                {
                    p.FoodResult = eFoodRecord.FoodResult;
                }
                else
                {
                    p.FoodResult = "";
                }
            });
            Session["Carbohydrates_RawList"] = List;
            this.Rep_DataList.DataSource = List;
            this.Rep_DataList.DataBind();

            UtilityUI.Init(this.UpdatePanel1);
        }

        protected void Btn_Del_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(this.txt_date.Text.Trim());
            List<E_FoodRecord> List = (List<E_FoodRecord>)Session["Carbohydrates_RawList"];
            List.ForEach(p =>
            {
                int RawID = int.Parse(p.RawID.ToString());
                //删除Vc含量检测
                E_CarbohydratesData eCarbohydratesData = new E_CarbohydratesData();
                eCarbohydratesData.CarbohydratesTime = date;
                eCarbohydratesData.RawId = RawID;
                tCarbohydratesData.RemoveByTime(eCarbohydratesData);

                //删除所有检测记录
                E_FoodRecord eFoodRecord = new E_FoodRecord();
                eFoodRecord.RawID = RawID;
                eFoodRecord.FoodTypeID = 3;//碳水化合物检测
                eFoodRecord.FoodTime = date;
                tFoodRecord.RemoveByFoodTime(eFoodRecord);
            });

            Session["Carbohydrates_RawList"] = null;
            this.Rep_DataList.DataSource = null;
            this.Rep_DataList.DataBind();
            UtilityUI.Alert(this.UpdatePanel1, "删除成功!");
        }

        protected void Btn_Export_Click(object sender, EventArgs e)
        {
            if (Session["Carbohydrates_RawList"] == null)
            {
                UtilityUI.Alert(this.UpdatePanel1, "请选择原料！");
                return;
            }

            List<E_FoodRecord> List = (List<E_FoodRecord>)Session["Carbohydrates_RawList"];
            DataTable dt = new DataTable();
            dt.Columns.Add("原料名称");
            dt.Columns.Add("检测时间");
            dt.Columns.Add("总糖量");
            List.ForEach(p =>
            {
                DataRow row = dt.NewRow();
                row["原料名称"] = p.RawName;
                row["检测时间"] = this.txt_date.Text.Trim();
                row["总糖量"] = p.FoodResult;
                dt.Rows.Add(row);
            });
            ExcelRender.RenderToExcel(dt, HttpContext.Current, Guid.NewGuid().ToString() + ".xls");
        }

        public List<DataTimeTitle> strDateList;
        public List<List<AssayDataList>> Assdatalist;
        protected void Btn_SearchFX_Click(object sender, EventArgs e)
        {
            string RawIDList = this.HidRawListId.Value;
            if (RawIDList == "")
            {
                UtilityUI.Alert(this.UpdatePanel1, "请选择原料！");
                return;
            }

            DateTime startTime = Convert.ToDateTime(this.txt_DateStart.Text);
            DateTime endTime = Convert.ToDateTime(this.txt_DateEnd.Text);
            if (this.HidShowId.Value == "0")
            {
                strDateList = new List<DataTimeTitle>();
                int index = 0;
                for (DateTime time = startTime; time <= endTime; time = time.AddDays(1))
                {
                    bool IsExists = false;
                    foreach (string RawId in RawIDList.Split(','))
                    {
                        E_FoodRecord eFoodRecord = new E_FoodRecord();
                        eFoodRecord.RawID = int.Parse(RawId);
                        eFoodRecord.FoodTypeID = 3; //检测
                        eFoodRecord.FoodTime = time;
                        eFoodRecord = tFoodRecord.GetFoodRecord(eFoodRecord);
                        if (eFoodRecord != null) //判断是否有检测记录
                        {
                            IsExists = true;
                            break;
                        }
                    }
                    if (IsExists)
                    {
                        strDateList.Add(new DataTimeTitle() { StrTime = time.ToString("MM月dd日"), Time = time, Index = index });
                        index += 1;
                    }
                }

                Assdatalist = new List<List<AssayDataList>>();
                foreach (string RawID in RawIDList.Split(','))
                {
                    List<AssayDataList> datalist = new List<AssayDataList>();
                    string strRawName = RawMaterial.Where("id=@0", int.Parse(RawID)).First().RAWName;
                    double SumScore = 0;
                    if (strDateList.Count > 0)
                    {
                        for (int i = 0; i < strDateList.Count; i++)
                        {
                            E_FoodRecord eFoodRecord = new E_FoodRecord();
                            eFoodRecord.RawID = int.Parse(RawID);
                            eFoodRecord.FoodTypeID = 3; //检测
                            eFoodRecord.FoodTime = strDateList[i].Time;
                            eFoodRecord = tFoodRecord.GetFoodRecord(eFoodRecord);
                            if (eFoodRecord != null)
                            {
                                SumScore = Convert.ToDouble(eFoodRecord.FoodResult);
                            }
                            datalist.Add(new AssayDataList() { AssayScore = SumScore, AssayTime = strDateList[i].Time, RawName = strRawName, RawId = int.Parse(RawID), IsSelectRaw = 0 });
                        }
                    }
                    else
                    {
                        datalist.Add(new AssayDataList() { AssayScore = SumScore, RawName = strRawName, RawId = int.Parse(RawID), IsSelectRaw = 1 });
                    }
                    Assdatalist.Add(datalist);
                }
            }
            else
            {
                UtilityUI.Load(this.UpdatePanel1, "SetShowPage();loadReport();");
            }
            UtilityUI.Load(this.UpdatePanel1, "SetShowPage();");
        }
    }
}